# Maintainer: Marian Buschsieweke <marian.buschsieweke@ovgu.de>

_targets="
	aarch64-none-elf
	arm-none-eabi
	mips-mti-elf
	msp430-elf
	riscv-none-elf
	"
# or1k-elf fails to build on 32-bit architectures
case "$CARCH" in
	x86_64|aarch64|ppc64le) _targets="$_targets or1k-elf"
esac
pkgname=newlib
pkgver=4.2.0.20211231
pkgrel=1
pkgdesc="C standard library implementation intended for use on embedded systems"
url="https://www.sourceware.org/newlib/"
makedepends="texinfo"
# s390x blocked by gcc-cross-embedded
arch="all !s390x"
license="GPL-2.0-only AND LGPL-2.1-only AND 0BSD AND BSD-3-Clause AND GPL-3.0-only AND LGPL-3.0-only"
source="
	https://sourceware.org/pub/newlib/newlib-$pkgver.tar.gz
	"
options="!check"

for target in $_targets; do
	targetnorm="${target//-/_}"
	subpackages="newlib-$target:$targetnorm $subpackages"
	makedepends="binutils-$target gcc-$target $makedepends"
done

# secfixes:
#   4.1.0-r0:
#     - CVE-2021-3420

# This package installs two version of the newlib library to remain compatible
# with projects expecting the GNU ARM Embedded Toolchain [1]. The nano version
# appends the postfix "_nano" to the libraries. The is not an arbitrary choice,
# it matches the specs in "/usr/arm-none-eabi/lib/nano.specs", which is
# part of the default newlib installation.
# [1]: https://launchpad.net/gcc-arm-embedded/

build() {
	for target in $_targets; do
		mkdir "$builddir/build-$target" "$builddir/build-$target-nano"
		# nano version
		cd "$builddir/build-$target-nano"
		export CFLAGS_FOR_TARGET="-Os -ffunction-sections -fdata-sections -fshort-wchar"
		"$builddir"/configure \
				--target=$target \
				--prefix=/usr/ \
				--infodir=/deleteme/info \
				--mandir=/deleteme/man \
				--htmldir=/deleteme/html \
				--pdfdir=/deleteme/pdf \
				--disable-newlib-fseek-optimization \
				--disable-newlib-fvwrite-in-streamio \
				--disable-newlib-supplied-syscalls \
				--disable-newlib-unbuf-stream-opt \
				--disable-newlib-wide-orient \
				--disable-nls \
				--enable-lite-exit \
				--enable-newlib-global-atexit \
				--enable-newlib-nano-formatted-io \
				--enable-newlib-nano-malloc \
				--enable-newlib-reent-small \
				--enable-newlib-retargetable-locking \
				CC_FOR_TARGET="$target"-gcc \
				GCC_FOR_TARGET="$target"-gcc \
				AR_FOR_TARGET="$target"-ar \
				AS_FOR_TARGET="$target"-as \
				LD_FOR_TARGET="$target"-ld \
				NM_FOR_TARGET="$target"-nm \
				OBJDUMP_FOR_TARGET="$target"-objdump \
				RANLIB_FOR_TARGET="$target"-ranlib \
				READELF_FOR_TARGET="$target"-readelf \
				STRIP_FOR_TARGET="$target"-strip
		make

		# regular version
		cd "$builddir/build-$target"
		export CFLAGS_FOR_TARGET="-O2 -ffunction-sections -fdata-sections"
		"$builddir"/configure \
				--host=$CHOST \
				--target=$target \
				--prefix=/usr \
				--infodir=/deleteme/info \
				--mandir=/deleteme/man \
				--htmldir=/deleteme/html \
				--pdfdir=/deleteme/pdf \
				--disable-newlib-supplied-syscalls \
				--disable-nls \
				--enable-newlib-io-c99-formats \
				--enable-newlib-io-long-long \
				--enable-newlib-register-fini \
				--enable-newlib-retargetable-locking \
				CC_FOR_TARGET="$target"-gcc \
				GCC_FOR_TARGET="$target"-gcc \
				AR_FOR_TARGET="$target"-ar \
				AS_FOR_TARGET="$target"-as \
				LD_FOR_TARGET="$target"-ld \
				NM_FOR_TARGET="$target"-nm \
				OBJDUMP_FOR_TARGET="$target"-objdump \
				RANLIB_FOR_TARGET="$target"-ranlib \
				READELF_FOR_TARGET="$target"-readelf \
				STRIP_FOR_TARGET="$target"-strip
		make
	done
}

package() {
	for target in $_targets; do
		depends="$depends newlib-$target"
	done
	mkdir -p "$pkgdir"
}

_install_subpkg() {
	target="${subpkgname#newlib-}"
	pkgdesc="A C standard library implementation intended for $target"
	depends="binutils-$target gcc-$target $depends"
	mkdir -p "$subpkgdir"
	# install nano version
	cd "$builddir/build-$target-nano"
	make -j1 DESTDIR="$subpkgdir" install
	# rename nano version libs
	cd "$subpkgdir/usr/$target/lib"
	find . -regex ".*\(c\|g\|m\|rdimon\|gloss\)\.a" | while read -r _file; do
		mv "$_file" "${_file%%.a}_nano.a"
	done
	mkdir -p "$subpkgdir/usr/$target/include/nano"
	mv "$subpkgdir/usr/$target/include/newlib.h" \
	   "$subpkgdir/usr/$target/include/nano/"

	# install regular version
	cd "$builddir/build-$target"
	make -j1 DESTDIR="$subpkgdir" install
	rm -rf "$subpkgdir/deleteme"
}

arm_none_eabi() {
	_install_subpkg
}

mips_mti_elf() {
	_install_subpkg
}

msp430_elf() {
	_install_subpkg
}

or1k_elf() {
	_install_subpkg
}

riscv_none_elf() {
	_install_subpkg
}

aarch64_none_elf() {
	_install_subpkg
}

sha512sums="
0c3efd7b74a6b8457a717cbb6aa6c5ff268eeaba375535465c6bd6502c3d32b54a9bc3ba7f2c6990f78e29152eee2f62acb39b674d24f9ddf440374a1ec9d2e8  newlib-4.2.0.20211231.tar.gz
"
